package gui;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import users.Manager;

public class DailySalesReport extends JPanel {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -4688566440180717969L;

	private JTextField storeText, dateText;
	private JButton createReport;
	private Manager manager;
	private JPanel main;
	private ResultSet tempReport;
	private JTable aTable;
	private JPanel aPanel;
	
	public DailySalesReport(Manager manager){
		this.manager = manager;
		main = new JPanel();
		main.setLayout(new BoxLayout(main, BoxLayout.PAGE_AXIS));
		main.setMaximumSize(getMaximumSize());
		inputPanel();
		add(main);
		setVisible(true);
	}
	private void inputPanel(){
		JPanel provide = new JPanel();
		provide.setLayout(new GridLayout(3, 2, 10, 10));
		provide.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
		
		provide.add(new JLabel("Store Name: "));
        storeText = new JTextField();
        storeText.setSize(new Dimension(100, 10));
        provide.add(storeText);
        
        provide.add(new JLabel("Date(YYYY-MM-DD): "));
        dateText = new JTextField();
        dateText.setSize(new Dimension(100, 10));
        provide.add(dateText);
        
        provide.add(new JLabel("Click to Create Daily Report"));
		
		createReport = new JButton("Create Report");
		createReport.setSize(new Dimension(100, 10));
		createReport.addActionListener(new createReport());
		provide.add(createReport);
		
		main.add(provide);
	}
	
	private void showTable(String name, String date) throws SQLException{
		
		aPanel = new JPanel(new FlowLayout());
		TitledBorder title = BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), "Daily Report");
		aPanel.setBorder(title);
		
		String[] columnNames = {"upc", 
								"category",
								"UnitPrice",
								"UnitsSold",
								"TotalValue"};
		
		tempReport = manager.createReport(name, date);
		aTable = rsToTable(tempReport, columnNames);
		//calculate(aTable);
		
		JScrollPane scrollPane = new JScrollPane(aTable);
		scrollPane.setSize(getMaximumSize());
		aPanel.add(scrollPane);
        main.add(aPanel);
	}
	
	/*private void calculate(JTable table) {

		int columnNum = 0;
		int rowNum = 1;
		int tempSold;
		int tempSold2;
		int totalSold = 0;
		int tempVal;
		int tempVal2;
		int totalValue = 0;
		JTable tempTable;
		Object[] columnNames ={"Total",
				"Units Sold",
				"Total Value"};
		String total = "Total is:";
		Object[] rowData = new Object[3];
		Object[][] allData = new Object[100][3];
		int i=0;
		
		if(table.getValueAt(rowNum, 1).toString().equals(table.getValueAt(rowNum+1, 1).toString())){
			tempSold = Integer.valueOf(table.getValueAt(rowNum, 4).toString());
			totalSold += tempSold;
			tempVal = Integer.valueOf(table.getValueAt(rowNum, 5).toString());
			totalValue += tempVal;

			
			while(table.getValueAt(rowNum, 1).toString().equals(table.getValueAt(rowNum+1, 1).toString())){
			tempSold2 = Integer.valueOf(table.getValueAt(rowNum+1, 4).toString());
			totalSold += tempSold2;
			tempVal2 = Integer.valueOf(table.getValueAt(rowNum+1, 5).toString());
			totalValue += tempVal2;
			rowNum++;
			}
			//rowData[0] = total;
			//rowData[1] = totalSold;
			//rowData[2] = totalValue;
			tempTable = new JTable();
		}
		else{
			rowNum = rowNum+1;
			calculate(table);
		}
	}*/
	
	public class createReport implements ActionListener{
		
		String namer;
		String finalDate;
		
		public void actionPerformed(ActionEvent e) {
			namer = storeText.getText();
			finalDate = dateText.getText();
			
            /*SimpleDateFormat fm = new SimpleDateFormat("dd/MM/yy");
            
			try {
				java.util.Date utilDate = fm.parse(dater);
				finalDate = new java.sql.Date(utilDate.getTime());
			} catch (ParseException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}*/  
			try {
				showTable(namer, finalDate);
				setVisible(true);
				validate();
			} catch (SQLException e1) {
				JOptionPane.showMessageDialog(createReport,
					    "Could not create daily report.\nPlease retry.", "Error",
					    JOptionPane.ERROR_MESSAGE);
				e1.printStackTrace();
				System.out.println("A problem occurred while creating the report");
			}
		}		
	}
	private static JTable rsToTable(ResultSet result, String[] columnNames) throws SQLException{
		List<Object[]> rows = new ArrayList<Object[]>();
		
		while (result.next()){
			Object[] row = new Object[columnNames.length+1];
			
			for (int i=0; i<columnNames.length; i++){
				Object value = result.getObject(columnNames[i]);
				row[i] = value;
			}			
			rows.add(row);
		}
		
		Object[][] data = new Object[rows.size()][columnNames.length+1];
		
		for (int i=0; i<rows.size(); i++){
			data[i] = rows.get(i);
		}
		
		JTable table = new JTable(data, columnNames);
		table.setPreferredScrollableViewportSize(new Dimension(500, 70));
        table.setFillsViewportHeight(true);
		
		return table;
	}
}
